package com.mxdx.edu.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import com.mxdx.edu.util.DictUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * 拦截xxs注入
 * 
 * @author wangyuan
 * @date 2016-11-24
 *
 */
@Configuration
@WebFilter(filterName = "xssFilter", urlPatterns = "/*")
public class XssFilter implements Filter {

    @Value("${staticHost}")
    private String staticHost;
    
    @Value("${server.context-path}")
    private String contextPath;

    @Value("${requestHost}")
    private String requestHost;



    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {

        XssHttpServletRequestWrapper xssRequest =
                new XssHttpServletRequestWrapper((HttpServletRequest) request);
        xssRequest.setAttribute("staticHost", staticHost);
        xssRequest.setAttribute("contextPath", contextPath);
        xssRequest.setAttribute("requestHost", requestHost);
        xssRequest.setAttribute("web_title", DictUtils.getDictLabel("1","web_title","梦想在线/梦想大学--- 汇集全球信息知识 服务梦想追求"));
        xssRequest.setAttribute("web_description", DictUtils.getDictLabel("1","web_description","梦想在线---汇集全球信息知识 服务梦想追求"));
        xssRequest.setAttribute("web_key_word", DictUtils.getDictLabel("1","web_key_word","梦想在线---汇集全球信息知识 服务梦想追求"));
        filterChain.doFilter(xssRequest, response);

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void destroy() {

    }

}
